package com.choupin.choupin;

import com.choupin.choupin.entity.Lottery;
import com.choupin.choupin.mapper.LotterypoolMapper;
import com.choupin.choupin.utils.redis.RedisMessageQueue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.support.collections.RedisMap;
import org.springframework.scheduling.annotation.EnableScheduling;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Set;

@SuppressWarnings("ALL")
@MapperScan("com.choupin.choupin.mapper,com.choupin.choupin.admin.mapper")
@SpringBootApplication
@EnableScheduling
public class ChoupinApplication {

    @Autowired
    LotterypoolMapper lotterypoolMapper;

    public static void main(String[] args) {

        SpringApplication.run(ChoupinApplication.class, args);
    }

//    服务器一旦关闭在从新启动，立马将redis会话中的信息删除
    @PostConstruct
    public void redisCleanup(){
        // 创建Jedis连接
        Jedis jedis = new Jedis("localhost", 6379);
        // 查找所有以"phone："开头的键值对
        Set<String> phoneKeys=jedis.keys("phone:*");
        // 删除所有用户手机号
        for (String key : phoneKeys) {
            jedis.del(key);
            System.out.println("服务器重新启动删除之前的用户: " + key);
        }
        //        删除所以用户密码
        Set<String> passwordKeys=jedis.keys("password:*");
        for (String key:passwordKeys ){
            jedis.del(key);
            System.out.println("服务器重新启动删除之前的用户密码: " + key);
        }
        //        删除所以用户
        Set<String> times=jedis.keys("time:*");
        for (String key:times ){
            jedis.del(key);
            System.out.println("服务器重新启动删除之前的用户登录时间: " + key);
        }

//        删除之前所以抽奖的消息队列
        List<Lottery> lotteries=lotterypoolMapper.selectAllLotterys();
        for (Lottery l:lotteries){
            RedisMessageQueue.deleteAllMessageQueue("lotteryUserQueue_"+l.getId());
            System.out.println("服务器重新启动删除之前所以抽奖的消息队列: lotteryUserQueue_"+l.getId());
        }



        // 关闭Jedis连接
        jedis.close();
    }


}
